package com.sright.bcard.enterprise.web.controller;

import com.sright.bcard.common.service.share.dto.user.CardConnectUserInfoDTO;
import com.sright.bcard.common.share.service.WxappService;
import com.sright.bcard.common.service.share.dto.content.CardDTO;
import com.sright.bcard.content.share.request.CardSaveParam;
import com.sright.bcard.content.share.service.UserService;
import com.sright.bcard.common.service.share.mvcresolver.EnableLoginAuth;
import com.sright.bcard.enterprise.share.request.CardListParam;
import com.sright.bcard.user.share.service.CardConnectUserInfoService;
import com.sright.common.response.SrightResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

/**
 * @author peter
 * @description
 * @date 2020/5/25
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController{

    @Autowired
    private UserService userService;

    @Autowired
    private WxappService wxappService;

    @Autowired
    private CardConnectUserInfoService cardConnectUserInfoService;

    /**
     * 创建名片用户
     * @param param
     * @return
     */
    @PostMapping("/cardAdd.json")
    @EnableLoginAuth
    public SrightResponse addCard(@Validated @RequestBody CardSaveParam param) {
        buildLoginUser(param);
        return userService.save(param);
    }

    /**
     * 修改名片用户
     * @param param
     * @return
     */
    @PutMapping("/cardPut.json")
    @EnableLoginAuth
    public SrightResponse updateCard(@Validated @RequestBody CardSaveParam param) {
        buildLoginUser(param);
        return userService.updateCard(param);
    }

    /**
     * 获取员工列表
     * @return
     */
    @GetMapping("/user.json")
    @EnableLoginAuth
    public SrightResponse cardList(CardListParam cardListParam) throws ParseException {
        build(cardListParam);
        return userService.cardList(cardListParam);
    }

    /**
     * 新增交接人
     * @param cardConnectUserInfoDTO
     * @return
     */
    @PostMapping("/connect.json")
    @EnableLoginAuth
    public SrightResponse addCardConnectUserInfo(@RequestBody CardConnectUserInfoDTO cardConnectUserInfoDTO) {
        return cardConnectUserInfoService.addCardConnectUserInfo(cardConnectUserInfoDTO);
    }

    /**
     * 删除员工查询员工列表
     * @param name
     * @return
     */
    @GetMapping("/connectUserList.json")
    @EnableLoginAuth
    public SrightResponse connectUserList(@RequestParam(value = "name") String name) {
        CardConnectUserInfoDTO cardConnectUserInfoDTO = new CardConnectUserInfoDTO();
        cardConnectUserInfoDTO.setName(name);
        cardConnectUserInfoDTO.setEnterpriseId(userContext.getEnterpriseId());
        return cardConnectUserInfoService.connectUserList(cardConnectUserInfoDTO);
    }

    /**
     * 企业开通后的到期天数
     * @return
     * @throws ParseException
     */
    @GetMapping("/expireDays.json")
    @EnableLoginAuth
    public SrightResponse expireDays() {
        return userService.expireDays(userContext.getEnterpriseId());
    }

    /**
     * 用户详情
     * @param id
     * @return
     */
    @GetMapping("/userDetail.json")
    @EnableLoginAuth
    public SrightResponse userDetail(@RequestParam("id") Long id) {
        return userService.userDetail(id);
    }

    /**
     * 获取预览码
     * @return
     */
    @GetMapping("/cardQrcode.json")
    @EnableLoginAuth
    public SrightResponse cardQrcode(Long id) throws Exception {
        return wxappService.getPreviewCode(id);
    }

    /**
     * 获取名片认领码
     * @return
     */
    @GetMapping("/qrcode.json")
    @EnableLoginAuth
    public SrightResponse qrcode(Integer id) throws Exception {
        String path = "pages/authorize/main?claimId=" + id;
        return wxappService.createQrCode(path);
    }

    /**
     * 名片解绑
     * @param cardDTO
     * @return
     */
    @PutMapping("/unbind.json")
    @EnableLoginAuth
    public SrightResponse unbind(@RequestBody CardDTO cardDTO) {
        return userService.unbind(cardDTO);
    }

    /**
     * 删除用户
     * @param CardDTO
     * @return
     */
    @DeleteMapping("/user.json")
    @EnableLoginAuth
    public SrightResponse user(@RequestBody CardDTO CardDTO) {
        return userService.deleteCard(CardDTO.getId());
    }

    /**
     * 获取邀请码
     * @return
     */
    @GetMapping("/inviteQrcode.json")
    @EnableLoginAuth
    public SrightResponse inviteQrcode() throws Exception {
        return wxappService.getInviteCode(userContext.getEnterpriseId());
    }

    /**
     * 获取名片设置
     * @return
     */
    @GetMapping("/setting.json")
    @EnableLoginAuth
    public SrightResponse setting() {
        return userService.getSetting(userContext.getEnterpriseId());
    }

    public void buildLoginUser(CardSaveParam param) {
        param.setUserId(userContext.getUserId());
        param.setToken(userContext.getToken());
        param.setEnterpriseId(userContext.getEnterpriseId());
    }

    private void build(CardListParam cardListParam) {
        cardListParam.setEnterpriseId(userContext.getEnterpriseId());
    }

}
